package wisp.gui;

//JFC
import wisp.game.MainGame;
import wisp.game.PlayerSprite;

import com.golden.gamedev.engine.BaseInput;
import com.golden.gamedev.gui.TButton;
import com.golden.gamedev.gui.TFloatPanel;
import com.golden.gamedev.gui.TLabel;
import com.golden.gamedev.gui.toolkit.FrameWork;
import com.golden.gamedev.gui.toolkit.UIConstants;

public class GUImanager extends FrameWork{

	
	private MainGame game;
	private PlayerSprite owner;
	
    private InfoWindow infoWindow;
    private StatsWindow statWindow;
    
    private SkillBar skillBar;
    private MenuBar menuBar;
    
    private SkillWindow skillWindow;
    private LootWindow lootWindow;
    private Inventory knapsackWindow;
    private TFloatPanel exitWindow;
    private BlurseWindow blessWindow;
    private BlurseWindow curseWindow;
    private NotificationWindow inboxWindow;
    private MessageWindow curseRequestWindow;
    private MessageWindow fRequest;
    private ComradeWindow comradeWindow;
    
    //for cursing
    private int selectedCurse;
    

    
    
    /****************************************************************/
    /*************************CONSTRUCTOR****************************/
    /****************************************************************/
	public GUImanager(BaseInput arg0, int width, int height, MainGame game_rcv, String hero_name, PlayerSprite p){
		super(arg0, width, height);
		game = game_rcv;
		owner = p;
		
		selectedCurse = 0;

		//init components here
        createInfoBox(hero_name);
        createSkillBar();
        createMenuBar();
        
        createSkillWindow();
        createStatBox();
        createInventoryWindow();
        createBlessWindow();
        createCurseWindow();
        createInboxWindow();
        createCurseRequestWindow();
        createFriendRequestWindow();
        createComradeWindow();
        
        createLootWindow();
        
        createExitDialog();
        
        //createCurseRequestWindow("TEST", "DESC", "YES", "NO");
		
	}

	/******************************************************************/
	/***********************CUSTOM UPDATE THINGY***********************/
	/******************************************************************/
	public void updateEberiting(){
		updateCharInfo();
		updateStats(owner);
		curseWindow.updateThis();
		inboxWindow.updateThis();
		
		///cheeeeck comrade window
		if(comradeWindow.isVisible()){
			for(int i = 0; i < comradeWindow.count; i++){
				if(comradeWindow.comradeCurseButton[i].clicked())
				{
					//CURSE SOMEONE LOLZ.
					game.submitCurseRequestFromVW(comradeWindow.comradeName[i].getText(), curseWindow.blurseName[i].getText());
					//CURSE COMMAND(target, curseWindow.getCurse[selectedCurse]);
					//samthing
					System.out.println("CURSE " + comradeWindow.comradeName[i].getText() + " with curse " + selectedCurse);
					comradeWindow.comradeCurseButton[i].clearClick();
				}
			}
		}
		
		
		this.update();
	}
	
	
	/*******************************************************************/
	/********************** CUSTOM FUNCTIONS ***************************/
	/*******************************************************************/
	public boolean hasClickedUI(){
		boolean result = false;
		
		if(infoWindow.isVisible()){
			if(game.checkPosMouse(infoWindow.getScreenX(), infoWindow.getScreenY(), infoWindow.getScreenX()+infoWindow.getWidth(), infoWindow.getScreenY()+infoWindow.getHeight()))
				result = true;
				//System.out.println("[SYSTEM] User has clicked UI :: infoWindow");
		}
		
		if(statWindow.isVisible()){
			if(game.checkPosMouse(statWindow.getScreenX(), statWindow.getScreenY(), statWindow.getScreenX()+statWindow.getWidth(), statWindow.getScreenY()+statWindow.getHeight()))
				result = true;
				//System.out.println("[SYSTEM] User has clicked UI :: statWindow");
		}
		if(skillBar.isVisible()){
			if(game.checkPosMouse(skillBar.getScreenX(), skillBar.getScreenY(), skillBar.getScreenX()+skillBar.getWidth(), skillBar.getScreenY()+skillBar.getHeight()))
				result = true;
				//System.out.println("[SYSTEM] User has clicked UI :: skillBar");
		}
		if(skillWindow.isVisible()){
			if(game.checkPosMouse(skillWindow.getScreenX(), skillWindow.getScreenY(), skillWindow.getScreenX()+skillWindow.getWidth(), skillWindow.getScreenY()+skillWindow.getHeight()))
				result = true;
				//System.out.println("[SYSTEM] User has clicked UI :: skillWindow");
		}
		if(exitWindow.isVisible()){
			if(game.checkPosMouse(exitWindow.getScreenX(), exitWindow.getScreenY(), exitWindow.getScreenX()+exitWindow.getWidth(), exitWindow.getScreenY()+exitWindow.getHeight()))
				result = true;
				//System.out.println("[SYSTEM] User has clicked UI :: exitWindow");
		}
		if(lootWindow.isVisible()){
			if(game.checkPosMouse(lootWindow.getScreenX(), lootWindow.getScreenY(), lootWindow.getScreenX()+ lootWindow.getWidth(), lootWindow.getScreenY()+lootWindow.getHeight()))
				result = true;
				//System.out.println("[SYSTEM] User has clicked UI :: exitWindow");
		}
		if(knapsackWindow.isVisible()){
			if(game.checkPosMouse(knapsackWindow.getScreenX(), knapsackWindow.getScreenY(), knapsackWindow.getScreenX()+knapsackWindow.getWidth(), knapsackWindow.getScreenY()+knapsackWindow.getHeight()))
				result = true;
		}
		
		if(menuBar.isVisible()){
			if(game.checkPosMouse(menuBar.getScreenX(), menuBar.getScreenY(), menuBar.getScreenX()+menuBar.getWidth(), menuBar.getScreenY()+menuBar.getHeight()))
				result = true;
		}
		if(curseWindow.isVisible()){
			if(game.checkPosMouse(curseWindow.getScreenX(), curseWindow.getScreenY(), curseWindow.getScreenX()+curseWindow.getWidth(), curseWindow.getScreenY()+curseWindow.getHeight()))
				result = true;
		}
		if(blessWindow.isVisible()){
			if(game.checkPosMouse(blessWindow.getScreenX(), blessWindow.getScreenY(), blessWindow.getScreenX()+blessWindow.getWidth(), blessWindow.getScreenY()+blessWindow.getHeight()))
				result = true;
		}
		if(inboxWindow.isVisible()){
			if(game.checkPosMouse(inboxWindow.getScreenX(), inboxWindow.getScreenY(), inboxWindow.getScreenX()+inboxWindow.getWidth(), inboxWindow.getScreenY()+inboxWindow.getHeight()))
				result = true;
		}
		
		
		return result;
	}
	
	public int curseRequestStatus(){
		
		int result = 0; //0 for no response yet, 1 for FIGHT!, 2 for LOLTAKAS
		
		if(curseRequestWindow.yesClicked()){
			System.out.println("[PLAYER] Curse accepted");
			result = 1;
			
			//reset curse window for next request
			//Clear "previous" curse notification
			curseRequestWindow.clearThis();
			curseRequestWindow.setVisible(false);
			game.getCurrentRequest().editCRequest("yes", "", game.getCurrentRequest().getCid());
		}
		else if(curseRequestWindow.noClicked()){
			System.out.println("[PLAYER] Curse challenge declined");
			result = 2;
			
			//reset curse window for next request
			//Clear "previous" curse notification
			curseRequestWindow.clearThis();
			curseRequestWindow.setVisible(false);
		}
		else
			result = 0;
		
		return result;
	}
	
	public boolean curse(String nagCurse){
		boolean status = true;
		
		curseRequestWindow.init(nagCurse,"FIGHT!","decline");
		
		curseRequestWindow.setVisible(true);
		
		System.out.println("[SYSTEM] created curse request.");
		return status;
	}
	
	public void showFriendRequest(Message m){
    	
		fRequest.clearThis();
		fRequest.init(m, "Accept", "Deny");
		fRequest.setMessageObj(m);
		fRequest.setVisible(true);
		
		game.setPendingFriendRequests(true);
	
	}
	
	public Message friendRequestStatus(){
		Message temp = null;
		
		if(fRequest.isVisible()){
			if(fRequest.yesClicked()){
				
				temp = fRequest.getMessageObj();
				temp.accepted = 1; //ACCEPT
				fRequest.clearThis();
				fRequest.setVisible(false);
			}
			else if(fRequest.noClicked()){
				
				temp = fRequest.getMessageObj();
				temp.accepted = 2; //REJECT
				fRequest.clearThis();
				fRequest.setVisible(false);
			}
		}
		
		
		return temp;
	}
	/***********************************************************************/
	/************************ WINDOW CREATE FUNCTIONS **********************/
	/***********************************************************************/
    //Chara
    private void createInfoBox(String name) {
    	String title = name;
    	infoWindow = new InfoWindow(title, false, false, 5, 5, 240, 100);
    	infoWindow.setVisible(true);
		this.add(infoWindow);
	}
    
    //Skill hotbar
    private void createSkillBar(){
    	skillBar = new SkillBar(owner);
    	skillBar.setVisible(true);
    	this.add(skillBar);
    }
    
    //Status box
    private void createStatBox() {

    	statWindow = new StatsWindow();
    	statWindow.setVisible(false);
    	this.add(statWindow);
			
	}
    
    //Skill window
    private void createSkillWindow(){
    	skillWindow = new SkillWindow(owner);
    	skillWindow.setVisible(false);
    	this.add(skillWindow);
    }
    
    //Dialog box ::: NPC
    public void createDialog(String text, String npcName){
    	TFloatPanel dialogWindow = new TFloatPanel(npcName, true, false, game.getHeight()/2, game.getWidth()/2, 200, 120);
    	
    	dialogWindow.setLayer(dialogWindow.getLayer() + 1);
    	dialogWindow.setVisible(true);
    	dialogWindow.setElastic(false);
    	
    	TLabel lbDialog = new TLabel("",5,5,120,50);
    	lbDialog.setText(text);
    	lbDialog.UIResource().put("Text Vertical Alignment Integer", UIConstants.TOP);
    	lbDialog.UIResource().put("Text Horizontal Alignment Integer", UIConstants.LEFT);
    	
    	dialogWindow.add(lbDialog);
    	
    	this.add(dialogWindow);
    }
    
    // Prompt maker
    public void createCurseRequestWindow(){
    		
    		String title = "NEW CURSE REQUEST";
    		String description = "";
    		String acceptPrompt = "";
    		String denyPrompt = "";
    		
    		curseRequestWindow = new MessageWindow(title, true, false, (game.getWidth()/2)-150, (game.getHeight()/2)-160, 300, 160);
    		curseRequestWindow.init(description, acceptPrompt, denyPrompt);
    		
    		curseRequestWindow.setVisible(false);
    		this.add(curseRequestWindow);

    }
    
    private void createFriendRequestWindow(){
    	String title = "FRIEND REQUEST";
    	String description = "";
    	String acceptPrompt = "Accept";
    	String denyPrompt = "Deny";
    	Message m = new Message("","","","",1);
    	
    	fRequest = new MessageWindow(title, true, true, (game.getWidth()/2)-125, (game.getHeight()/2)-160, 250, 160);
    	fRequest.init(m, acceptPrompt, denyPrompt);
    	
    	fRequest.setVisible(false);
    	this.add(fRequest);
    	
    }
    
    //Exit game prompt
    public void createExitDialog(){
    	exitWindow = new TFloatPanel ("Exit Game?", true, false, game.getHeight()/2, game.getWidth()/2, 140, 80);
    	
    	exitWindow.setLayer(exitWindow.getLayer() + 1);
    	exitWindow.setVisible(false);
    	exitWindow.setElastic(false);
    	
    	TButton tYes = new TButton("Yes", 4,4,60,40){
    		public void doAction(){
    			System.out.println("[SYSTEM] client exiting.");
    			System.exit(1);
    		}
    	};
    	
    	TButton tNo = new TButton("No", 66,4,60,40){
    		public void doAction(){
    			exitWindow.setVisible(false);
    		}
    	};
    	
    	exitWindow.add(tYes);
    	exitWindow.add(tNo);
    	this.add(exitWindow);
    }
    
    //Corpse loot window
    public void createLootWindow(){
    	lootWindow = new LootWindow(owner);
    	
    	lootWindow.setVisible(false);
    	this.add(lootWindow);
    }
    
    //Knapsack window
    public void createInventoryWindow(){
    	knapsackWindow = new Inventory(owner);
    	
    	knapsackWindow.setVisible(false);
    	knapsackWindow.isElastic();
    	
    	
    	this.add(knapsackWindow);
    }
    
    //Menu bar
    public void createMenuBar(){
    	menuBar = new MenuBar(owner, this);
    	
    	menuBar.setVisible(true);
    	this.add(menuBar);
    }
    
    //Bless window
    public void createBlessWindow(){
    	blessWindow = new BlurseWindow("Bless", owner, this);
    	
    	blessWindow.setVisible(false);
    	this.add(blessWindow);
    }
    
    //Curse window
    public void createCurseWindow(){
    	curseWindow = new BlurseWindow("Curse", owner, this);
    	
    	curseWindow.addSkill(0,"Cockatrice", "10", "Cockatrice is th despised pet of the goddess Yellesh from the clan of Bellum.");
    	curseWindow.addSkill(1,"Goblin", "10", "Goblins are creatures in servitude to the Nelah, a god from the Fascia clan.");
    	curseWindow.addSkill(2,"Troll", "10 ", "Trolls are related to Goblins but were given as slaves to the goddess Prisskela.");
    	
    	
    	curseWindow.setVisible(false);
    	this.add(curseWindow);
    }
    
    //Character Inbox
	private void createInboxWindow() {
		inboxWindow = new NotificationWindow(owner, this);
		
		inboxWindow.setVisible(false);
		this.add(inboxWindow);
		
	}
	
	public void createMessage(Message m){
		
		if(m.type == 2) //Friend request
			showFriendRequest(m);
		else
		{
			MessageWindow n = new MessageWindow(m.title,true, true, 300, 160, 300, 160);
			n.init(m.sender, m.messagebody);
			
			n.setVisible(true);
			this.add(n);
		}
	}
    
	private void createComradeWindow() {
		comradeWindow = new ComradeWindow(owner, this);
		comradeWindow.setVisible(false);
		this.add(comradeWindow);
		
	}
	
    /**********************************************************************/
    /*********************** WINDOW VISIBILITY FUNCTIONS ******************/
    /**********************************************************************/
    public void ToggleInfoWindow(){
    	if(!infoWindow.isVisible())
    	{
    		infoWindow.setVisible(true);
    		
    	}
    	
    	else
    		infoWindow.setVisible(false);
    }
    
    public void ToggleLootWindow(){
    	if(!lootWindow.isVisible())
    	{
    		lootWindow.setVisible(true);
    		
    	}
    	
    	else
    		lootWindow.setVisible(false);
    }
    
    public void ToggleKnapsack(){
    	if(!knapsackWindow.isVisible())
    	{
    		knapsackWindow.setVisible(true);
    		
    	}
    	
    	else
    		knapsackWindow.setVisible(false);
    }
    
    public void ToggleStatWindow(){
    	if(!statWindow.isVisible())
    	{
    		statWindow.setVisible(true);
    		
    	}
    	
    	else
    		
    		statWindow.setVisible(false);
    }
    
    public void ToggleSkillWindow(){
    	if(!skillWindow.isVisible())
    	{	
    		skillWindow.setVisible(true);
    		
    	}
    	else
    		
    		skillWindow.setVisible(false);
    }
    
    public void ToggleBlessWindow(){
    	if(!blessWindow.isVisible())
    	{	
    		blessWindow.setVisible(true);
    		
    	}
    	else
    		
    		blessWindow.setVisible(false);
    }
    
    public void ToggleInbox(){
    	if(!inboxWindow.isVisible())
    	{	
    		inboxWindow.setVisible(true);
    		
    	}
    	else
    		inboxWindow.setVisible(false);
    }
    
    public void ToggleCurseWindow(){
    	if(!curseWindow.isVisible())
    	{	
    		curseWindow.setVisible(true);
    		
    	}
    	else
    		
    		curseWindow.setVisible(false);
    }
    
    public void ToggleExitWindow(){
    	if(!exitWindow.isVisible())
    	{	
    		exitWindow.setVisible(true);
    		
    	}
    	else
    		
    		exitWindow.setVisible(false);
    }
    
    public void ToggleComradeWindow(){
    	if(!comradeWindow.isVisible())
    	{	
    		comradeWindow.setVisible(true);
    		
    	}
    	else
    		
    		comradeWindow.setVisible(false);
    }
    
    /********************************************************************/
    /************ INFOWINDOW FUNCTIONS **********************************/
    /********************************************************************/

    public void updateHP(int h){
    	infoWindow.hPSet(h);
    }
    
    public void updateMP(int h){
    	infoWindow.mPSet(h);
    }
    
    public void updateStats(PlayerSprite p){
    	statWindow.updateStats(p);
    }
    
	
	public void updateCharInfo(){
		updateHP( new Double(new Double(owner.getCurrentHP()).doubleValue() / new Double(owner.getMaxHP()).doubleValue() * 100).intValue() );
		updateMP( new Double(new Double(owner.getCurrentMP()).doubleValue() / new Double(owner.getMaxMP()).doubleValue() * 100).intValue() );
		infoWindow.expSet(owner.getCurrentEXP());
	}

    

    /*********************************************************************/
    /************** SKILLBAR FUNCTIONS ***********************************/
    /*********************************************************************/
    public void useSkill1(){
    	skillBar.btn1.doClick(20);
    }
    
    public void useSkill2(){
    	skillBar.btn2.doClick(20);
    }
    
    public void useSkill3(){
    	skillBar.btn3.doClick(20);
    }


    
    /**********************************************************************/
    /**************** GETTERS/SETTER FUNCTIONS ****************************/
    /**********************************************************************/
    public SkillWindow getSkillWindow(){
    	return skillWindow;
    }
    
    public NotificationWindow getInbox(){
    	return inboxWindow;
    }
    
	public MainGame getGame(){
		return game;
	}
	
    public SkillBar getSkillbar(){
    	return skillBar;
    }
    
    public ComradeWindow getComradeList(){
    	return comradeWindow;
    }
    
    public void setCurseSelected(int n){
    	selectedCurse = n;
    }
}
